<?php
/**
 * 搜索
 * @link https://www.leadshop.vip/
 * @copyright Copyright ©2020-2021 浙江禾成云计算有限公司
 */

namespace leadmall\api;

use leadmall\Map;
use framework\common\BasicController;
use Yii;
use yii\web\ForbiddenHttpException;
use modules\order\models\Order;

class SumcountController extends BasicController implements Map
{

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        unset($actions['index']);
        return $actions;
    }


    public function actionIndex()
    {
        $merchant_id = 1;
        $AppID       = Yii::$app->params['appid'];
        $where       = ['order.merchant_id' => $merchant_id, 'order.AppID' => $AppID];
        $data_list = ['arrival_all' => [],'arrival_today' => [],'arrival_toweek' => [],'arrival_tomon' => [],'verify_all' => [],'verify_today' => [],'verify_toweek' => [],'verify_tomon' => []];

        foreach ($data_list as $key => &$value) {
            switch ($key) {
                case 'arrival_all': //到店時間自提
                    $w = ['and',['<>', 'order.verify_code',''],['order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'arrival_today': //到店時間自提今日
                    $time_start=strtotime(date('Ymd').'00:00:00');
                    $time_end=strtotime(date('Ymd').'23:59:59');
                    $w = ['and',['<>', 'order.verify_code',''],['>=', 'order.created_time',$time_start],['<=', 'order.created_time',$time_end],['order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'arrival_toweek': //到店時間自提本周
                    $we = date('w',time()) - 1;
                    $time_start = time() - $we * 60 * 60 * 24;         //星期一的时间戳
                    $time_end = time() + (6 - $we) * 60 * 60 * 24; //星期天的时间戳
                    $w = ['and',['<>', 'order.verify_code',''],['>=', 'order.created_time',$time_start],['<=', 'order.created_time',$time_end],['order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'arrival_tomon': //到店時間自提本月
                    $time_start = strtotime(date('Y-m',time()));
                    $time_end = strtotime(date('Y-m',time()).'+1 month');
                    $w = ['and',['<>', 'order.verify_code',''],['>=', 'order.created_time',$time_start],['<', 'order.created_time',$time_end],['order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'verify_all': //核銷記錄
                    $w = ['and',['<>', 'order.verify_time', 0],['<>', 'order.verify_code',''],['<>', 'order.verify_user_id', 0],['order.status' => 205,'order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'verify_today': //核銷記錄今日
                    $time_start=strtotime(date('Ymd').'00:00:00');
                    $time_end=strtotime(date('Ymd').'23:59:59');
                    $w = ['and',['<>', 'order.verify_code',''],['<>', 'order.verify_user_id',0],['<>', 'order.verify_code',''],['>=', 'order.verify_time',$time_start],['<=', 'order.verify_time',$time_end],['order.status' => 205,'order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'verify_toweek': //核銷記錄本周
                    $we = date('w',time()) - 1;
                    $time_start = time() - $we * 60 * 60 * 24;         //星期一的时间戳
                    $time_end = time() + (6 - $we) * 60 * 60 * 24; //星期天的时间戳
                    $w = ['and',['<>', 'order.verify_code',''],['<>', 'order.verify_user_id',0],['<>', 'order.verify_code',''],['>=', 'order.verify_time',$time_start],['<=', 'order.verify_time',$time_end],['order.status' => 205,'order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
                case 'verify_tomon': //核銷記錄本月
                    $time_start = strtotime(date('Y-m',time()));
                    $time_end = strtotime(date('Y-m',time()).'+1 month');
                    $w = ['and',['<>', 'order.verify_code',''],['<>', 'order.verify_user_id',0],['<>', 'order.verify_code',''],['>=', 'order.verify_time',$time_start],['<', 'order.verify_time',$time_end],['order.status' => 205,'order.currentTakeType'=>0,'order.is_recycle' => 0]];
                    break;
            }

            $w     = ['and', $where, $w];
            $value = \order\models\Order::find()
                ->alias('order')
                ->joinWith([
                    'buyer as buyer',
                    'goods as goods',
                    'user as user',
                ])
                ->where($w)
                ->groupBy(['order.id'])
                ->count();
        }

        return $data_list;
    }



}
